<?php

namespace app\web\controller;

use app\common\controller\Base;

use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\Shared\Html;
use app\common\component\ErrorCode;
use app\common\component\Param;

class Index extends Base
{
    /**
     * 首页渲染
     * @return mixed
     */
    public function index()
    {
        $this->assign('title', '缘路于此网站-首页');
        return $this->show();
    }

    /**
     * 小练习页面渲染
     * @return mixed
     */
    public function practice()
    {
        $this->assign('title', '缘路于此网站-小实践');
        return $this->show();
    }

    /**
     * 百度编辑器编辑下载word
     * @return mixed
     */
    public function ueditor()
    {
        $this->assign('title', '缘路于此网站-百度编辑器');
        return $this->show();
    }

    /**
     * elasticsearch简易热词
     * @return mixed
     */
    public function elasticsearch()
    {
        $noncestr = \app\web\logic\CommonLogic::createNoncestr($this->request->controller(), $this->request->action());
        $this->assign('title', '缘路于此网站-elasticsearch简易热词');
        $this->assign('noncestr', $noncestr);
        return $this->show();
    }

    /**
     * excel编排工具-页面渲染
     * @return mixed
     */
    public function excel()
    {
        $this->assign('title', '缘路于此网站-excel编排工具');
        return $this->show();
    }

    /**
     * 生成导出word文件
     * @return string
     * @throws \PhpOffice\PhpWord\Exception\Exception
     */
    public function formWord()
    {
        $html = Param::$validParams['content'];
        $phpWord = new PhpWord();
        $section = $phpWord->addSection();
        Html::addHtml($section, $html, false, false);
        $objWriter = IOFactory::createWriter($phpWord, 'Word2007');
        $filename = RUNTIME_PATH . 'word/' . date('ymdHis') . rand(1, 10) . '.docx';
        $objWriter->save($filename);
        return $this->responseData(['filePath' => $filename]);
    }

    /**
     * 下载ueditor编辑的的文件
     */
    public function downloadUeditorWord()
    {
        $this->downloadWord($_GET['filePath']);
    }

    /**
     * 添加mongo 和 elasticserch数据
     * @return string
     */
    public function addElasticsearch()
    {
        $elasticsearchLogic = new \app\web\logic\ElasticsearchLogic();
        $elasticsearchLogic->addElasticsearch();
        return $this->responseSuccess();
    }

    /**
     * 列表数据
     * @return string
     */
    public function listElasticsearch()
    {
        $elasticsearchLogic = new \app\web\logic\ElasticsearchLogic();
        $list = $elasticsearchLogic->listElasticsearch();
        return $this->responseData($list);
    }

    /**
     * 生成excel
     */
    public function formatExcel()
    {
        $excelComponentObj = new \app\common\component\Excel();
        $filename = $excelComponentObj->formatExcel('change_excel', Param::$validParams['excel_data']);
        return $this->responseData(['name' => $filename]);
    }

    /**
     * 下载excel
     */
    public function downloadExcel()
    {
        $excelComponentObj = new \app\common\component\Excel();
        $excelComponentObj->downloadExcel($_GET['filePath']);
    }

    /**
     * swoole聊天室
     * @return mixed
     */
    public function swooleChat()
    {
        $this->assign('title', '缘路于此网站-swoole聊天室');
        return $this->show();
    }

    /**
     * 弹幕系统
     * @return mixed
     */
    public function danmu()
    {
        $this->assign('title', '缘路于此网站-弹幕功能');
        return $this->show();
    }

    public function test()
    {
        $this->assign('title', 'test');
        return $this->show();
        var_dump($_SERVER['SERVER_ADDR']);exit();
        $a = 'lcm.docx';
        $b = str_replace('.docx', '_back.docx', $a);
        var_dump($b);
    }

    /**
     * 这是一个从自己服务器读取资源的，的防盗链思路
     * 思路:  (1): img的src属性不是直接的资源地址，而是一个get访问接口的方式 例如 <img src="接口?时间戳和base64加密的资源" />
     *       (2): 服务器用fread()的方式读取文件，然后进行echo输出
     *       (3): 七牛有 防盗链(referer)和时间防盗链的说法，更好
     */
    public function showImg()
    {
        $url = isset($_GET['url']) ? $_GET['url'] : '404';
        $file_path = '/mydata/www/www_lcm/public/static/img/web/Index/index/q_logo.jpeg';
        $fp = fopen($file_path, 'r');
        if ($fp) {
            $a = fread($fp, filesize($file_path));
            echo $a;
            fclose($fp);
        } else {
            var_dump('失败');
        }
    }
}
